﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-20>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：内部转账
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ZhangWuGL
{
    public partial class NeiBuZZ : BasePage
    {
        #region 属性
        private DataTable dtNeiBuZZ
        {
            get
            {
                return ViewState["dtNeiBuZZ"] as DataTable;
            }
            set
            {
                ViewState["dtNeiBuZZ"] = value;
            }
        }

        /// <summary>
        /// 内部转账信息id
        /// </summary>
        private int NeiBuZZXX_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["NeiBuZZXX_ID"]);
            }
            set
            {
                ViewState["NeiBuZZXX_ID"] = value;
            }
        }

        #endregion

        #region 变量
       
        /// <summary>
        /// 用户信息BLL
        /// </summary>
        QS_YG_YONGHUXXBLL YHXXBLL = new QS_YG_YONGHUXXBLL();
       
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        /// <summary>
        /// 日志操作类
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }


        }

        /// <summary>
        /// 取消内部转账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuXiaoNBZZ_Click(object sender, EventArgs e)
        {
            DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + this.NeiBuZZXX_ID);
            int bh_out = Convert.ToInt32(dr[0].ItemArray[4]);
            int bh_in = Convert.ToInt32(dr[0].ItemArray[5]);
            int clr = Convert.ToInt32(dr[0].ItemArray[3]);
            decimal je = Convert.ToInt32(dr[0].ItemArray[6]);
            int? result = -1;
            //_spHelper.P_QS_ZW_NEIBUZZ(bh_out, bh_in, "", clr, je, this.NeiBuZZXX_ID, out result);
            if (result == 1)
            {
                InitData();
            }
        }

        /// <summary>
        /// 确认转账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQueDing_Click(object sender, EventArgs e)
        {
            //bool r_out = false;
            //bool r_in = false;
            //int bh_out = 0, bh_in = 0;
            //if (Validate())
            //{
            //    //先验证是否有加价开账记录
            //    string yhh_out = txtYongHuH_out2.Text;
            //    int ym_out = Convert.ToInt32(txtShuiLiangNY_out2.Text);
            //    DataTable dt_out = YYZBLL.SelectQS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh_out, ym_out), null);
            //    //验证通过后，将该用户号对应的消息显示在输入框内
            //    if (dt_out != null && dt_out.Rows.Count > 0)
            //    {
            //        decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_YINGSHOUJE"]);
            //        decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHISHOUJE"]);
            //        bh_out = Convert.ToInt32(dt_out.Rows[0]["I_FEIYONGBH"]);
            //        DataTable dt = YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_out), null);
            //        string yhm_out = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            //        txtYongHuM_out2.Text = yhm_out;
            //        txtYingShouJE_out2.Text = ysje_out.ToString();
            //        txtShiShouJE_out2.Text = ssje_out.ToString();
            //        r_out = true;
            //    }
            //    else
            //    {
            //        PageTools.AlertMsg(this, "您输入的转出费用信息没有找到加价开账记录");
            //    }
            //    string yhh_in = txtYongHuH_in2.Text;
            //    int ym_in = Convert.ToInt32(txtShuiLiangNY_in2.Text);
            //    DataTable dt_in = YYZBLL.SelectQS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh_in, ym_in), null);
            //    if (dt_in != null && dt_in.Rows.Count > 0)
            //    {
            //        decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_YINGSHOUJE"]);
            //        decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHISHOUJE"]);
            //        DataTable dt = YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_in), null);
            //        bh_in = Convert.ToInt32(dt_in.Rows[0]["I_FEIYONGBH"]);
            //        string yhm_in = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            //        txtYongHuM_in2.Text = yhm_in;
            //        txtYingShouJE_in2.Text = ysje_in.ToString();
            //        txtShiShouJE_in2.Text = ssje_in.ToString();
            //        r_in = true;
            //    }
            //    else
            //    {
            //        PageTools.AlertMsg(this, "您输入的转入费用信息没有找到加价开账记录");
            //    }
            //}
            //else
            //{
            //    PageTools.AlertMsg(this, "请先正确输入");
            //}
            ////显示完后，再执行确认转账的操作
            //if (r_out && r_in)
            //{
            //    if (txtZhuanZhangJE.Text.Trim() == "")
            //    {
            //        PageTools.AlertMsg(this, "转账金额不能为空，请先输入");
            //    }
            //    else if (Convert.ToDecimal(txtZhuanZhangJE.Text.Trim()) > Convert.ToDecimal(txtShiShouJE_out2.Text.Trim()))
            //    {
            //        PageTools.AlertMsg(this, "转账金额不能大于实收金额");
            //    }
            //    else
            //    {
            //        int userid = LoginInfo.CurrentUser.UserID;
            //        int? result = -1;
            //        decimal zzje = Convert.ToDecimal(txtZhuanZhangJE.Text);
            //        _spHelper.P_QS_ZW_NEIBUZZ(bh_out, bh_in, "", userid, zzje, 0, out result);
            //        if (result == 1)
            //        {
            //            PageTools.AlertMsg(this, "确认转账成功");
            //        }
            //        else
            //        {
            //            PageTools.AlertMsg(this, "转账失败");
            //        }
            //    }
            //}
        }

        /// <summary>
        /// 转账信息列表，行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvZhuanZhangLB_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.dtNeiBuZZ != null && this.dtNeiBuZZ.Rows.Count > 0)
            {
                this.NeiBuZZXX_ID = Convert.ToInt32(this.gvZhuanZhangLB.GetRowValues(this.gvZhuanZhangLB.FocusedRowIndex, "ID"));
                SetDataTab1();
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }
        #endregion

        #region 方法

        /// <summary>
        /// 页面加载
        /// </summary>
        private void InitData()
        {
            txtZWNY.Text = BusinessTool.GetSLYMorZWNY(2, BusinessTool.WType).ToString();
            
        }

        /// <summary>
        /// 加载列表下方输入框信息
        /// </summary>
        private void SetDataTab1()
        {
            //DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + this.NeiBuZZXX_ID);
            //if (dr.Length > 0)
            //{
            //    //转出费用信息
            //    int bh_out = Convert.ToInt32(dr[0].ItemArray[4]);
            //    DataTable dt_out = YYZBLL.SelectQS_ZW_YINGYEZsDynamic(string.Format("I_FEIYONGBH={0}", bh_out), null);
            //    if (dt_out != null && dt_out.Rows.Count > 0)
            //    {
            //        string yhh_out = dt_out.Rows[0]["S_YONGHUH"].ToString();
            //        decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_YINGSHOUJE"]);
            //        decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHISHOUJE"]);
            //        int ym_out = Convert.ToInt32(dt_out.Rows[0]["I_Y"]) * 100 + Convert.ToInt32(dt_out.Rows[0]["I_M"]);
            //        DataTable dt = YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_out), null);
            //        string yhm_out = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            //        txtYongHuH_out.Text = yhh_out;
            //        txtYongHuM_out.Text = yhm_out;
            //        txtShuiLiangNY_out.Text = ym_out.ToString();
            //        txtYingShouJE_out.Text = ysje_out.ToString();
            //        txtShiShouJE_out.Text = ssje_out.ToString();
            //    }

            //    //转入费用信息
            //    int bh_in = Convert.ToInt32(dr[0].ItemArray[5]);
            //    DataTable dt_in = YYZBLL.SelectQS_ZW_YINGYEZsDynamic(string.Format("I_FEIYONGBH={0}", bh_in), null);
            //    if (dt_in != null && dt_in.Rows.Count > 0)
            //    {
            //        string yhh_in = dt_in.Rows[0]["S_YONGHUH"].ToString();
            //        decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_YINGSHOUJE"]);
            //        decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHISHOUJE"]);
            //        int ym_in = Convert.ToInt32(dt_in.Rows[0]["I_Y"]) * 100 + Convert.ToInt32(dt_in.Rows[0]["I_M"]);
            //        DataTable dt = YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_in), null);
            //        string yhm_in = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            //        txtYongHuH_in.Text = yhh_in;
            //        txtYongHuM_in.Text = yhm_in;
            //        txtShuiLiangNY_in.Text = ym_in.ToString();
            //        txtYingShouJE_in.Text = ysje_in.ToString();
            //        txtShiShouJE_in.Text = ssje_in.ToString();
            //    }
            //}
        }

        /// <summary>
        /// 验证是否输入框都显示不为空
        /// </summary>
        /// <returns></returns>
        private bool Validate()
        {
            bool res = true;
            if (txtYongHuH_out2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtShuiLiangNY_out2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtYongHuH_in2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtShuiLiangNY_in2.Text.Trim() == "")
            {
                res = false;
            }

            return res;
        }

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int result = -1;
            string msg = "";

            string acc1 = this.hdCollection.Get("yhh").ToString();
            int acc2 = Convert.ToInt32(this.hdCollection.Get("ym"));
            if (e.Parameter == "setinfo_out")
            {
                //先验证是否有开账记录
                DataTable dt_out = _spHelper.P_QSZW_NEIBUZZ_XX(BusinessTool.WType, acc1, acc2).Tables[0];
                //YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format(@"I_JLZT=0 AND I_WTYPE={0} AND S_YONGHUH={2} AND S_YONGHUH IN 
//                        (SELECT S_YONGHUH FROM QS_SF_QUEDINGZD B WHERE I_JLZT=0 AND (B.I_Y*100_B.I_M)={1} AND EXISTS 
//                        (SELECT 1 FROM QS_SF_QIANFEI C WHERE C.I_JLZT=0 AND B.S_ZHANGDANBH=C.S_ZHANGDANBH AND C.I_WTYPE={0}))", BusinessTool.WType, ym, yhh), null);
                //验证通过后，将该用户号对应的消息显示在输入框内
                if (dt_out != null && dt_out.Rows.Count > 0)
                {
                    result = 1;
                }
                if (result == 1)
                {
                    decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_JIHUAJE"]);
                    decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHIYONGJE"]);
                    int bh_out = Convert.ToInt32(dt_out.Rows[0]["S_FEIYONGBH"]);
                    string yhm_out = dt_out.Rows[0]["S_YONGHUM"].ToString();
                    int r_out = 1;
                    msg = string.Join("|", Convert.ToString(result), Convert.ToString(yhm_out), Convert.ToString(ysje_out), Convert.ToString(ssje_out), Convert.ToString(r_out), Convert.ToString(bh_out));
                }
                else
                {
                    msg = string.Join("|", Convert.ToString(result));
                }

            }
            else if (e.Parameter == "setinfo_in")
            {
                //先验证是否有开账记录 
                DataTable dt_in = _spHelper.P_QSZW_NEIBUZZ_XX(BusinessTool.WType, acc1, acc2).Tables[0];
                    //YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format(@"I_JLZT=0 AND I_WTYPE={0} AND S_YONGHUH={2} AND S_YONGHUH IN 
                      //  (SELECT S_YONGHUH FROM QS_SF_QUEDINGZD B WHERE I_JLZT=0 AND (B.I_Y*100_B.I_M)={1} AND EXISTS 
                        //(SELECT 1 FROM QS_SF_QIANFEI C WHERE C.I_JLZT=0 AND B.S_ZHANGDANBH=C.S_ZHANGDANBH AND C.I_WTYPE={0}))", BusinessTool.WType, ym, yhh), null);
                //验证通过后，将该用户号对应的消息显示在输入框内
                if (dt_in != null && dt_in.Rows.Count > 0)
                {
                    result = 1;
                }
                if (result == 1)
                {
                    decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_JIHUAJE"]);
                    decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHIYONGJE"]);
                    int bh_in = Convert.ToInt32(dt_in.Rows[0]["S_FEIYONGBH"]);
                    string yhm_in = dt_in.Rows[0]["S_YONGHUM"].ToString();
                    int r_in = 1;
                    msg = string.Join("|", Convert.ToString(result), Convert.ToString(yhm_in), Convert.ToString(ysje_in), Convert.ToString(ssje_in), Convert.ToString(r_in), Convert.ToString(bh_in));
                }
                else
                {
                    msg = string.Join("|", Convert.ToString(result));
                }

            }
            else if (e.Parameter == "qdzz")
            {
                int zwym = BusinessTool.GetSLYMorZWNY(2, BusinessTool.WType);
                string yhh1 = txtYongHuH_out2.Text;
                string yhh2 = txtYongHuH_in2.Text;
                int slym1 = Convert.ToInt32(txtShuiLiangNY_out2.Text);
                int slym2 = Convert.ToInt32(txtShuiLiangNY_in2.Text);
                string userid = LoginInfo.CurrentUser.UserID.ToString();
                string result2 = "0";
                decimal zzje = Convert.ToDecimal(txtZhuanZhangJE.Text);
                _spHelper.P_QSZW_NEIBUZZ(1, BusinessTool.WType, yhh1, yhh2, acc1, acc2.ToString(), slym1, slym2, zwym, zzje, userid, out result2);
                msg = string.Join("|", Convert.ToString(result2));
            }

            else if (e.Parameter == "qxnbzz")
            {
                string userid = LoginInfo.CurrentUser.UserID.ToString();
                DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + acc1);
                string yhh1 = dr[0].ItemArray[0].ToString();
                string yhh2 = dr[0].ItemArray[1].ToString();
                int ZWYM = Convert.ToInt32(dr[0].ItemArray[2]);
                decimal je = Convert.ToInt32(dr[0].ItemArray[3]);
                string bh_out = dr[0].ItemArray[4].ToString();
                string bh_in = dr[0].ItemArray[5].ToString();
                string result2 = "0";
                _spHelper.P_QSZW_NEIBUZZ(2, BusinessTool.WType, yhh1, yhh2, bh_out, bh_in, 0, 0, ZWYM, je, userid, out result2);
                msg = string.Join("|", Convert.ToString(result2));
            }
            e.Result = e.Parameter + "|0|" + msg;
        }

        /// <summary>
        /// 列表刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvZhuanZhangLB_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXunTab2_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载列表数据
        /// </summary>
        private void LoadData()
        {
            string yhh1 = txtYongHuH1.Text;
            string yhh2 = txtYongHuH2.Text;
            int zwym = Convert.ToInt32(txtZWYM2.Text);
            this.dtNeiBuZZ = _spHelper.P_QSZW_NEIBUZZLB(yhh1, yhh2, zwym).Tables[0];
            gvZhuanZhangLB.DataSource = this.dtNeiBuZZ;
            gvZhuanZhangLB.DataBind();
        }
        #endregion
       
    }
}